<template>
	<div class="app-container">
		<el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
			<el-form-item label="申请文件记录号" prop="requestNum">
				<el-input v-model="queryParams.requestNum" maxlength="6" placeholder="请输入申请文件记录号" clearable />
			</el-form-item>

			<el-form-item label="证书状态" prop="enabled">
				<el-select v-model="queryParams.enabled" placeholder="请选择" clearable>
					<el-option v-for="dict in public_cert_status" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>

			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<div class="base-container mb-50px">
			<el-row :gutter="10" class="mb-10px">
				<el-col :span="1.5">
					<el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['bus:codeManagementConfig:add']">
						新增
					</el-button>
				</el-col>

				<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
			</el-row>

			<el-table border v-loading="loading" :data="itemList">
				<el-table-column label="申请文件记录号" align="center" prop="requestNum" show-overflow-tooltip />
				<el-table-column label="证书失效日期" align="center" prop="caPubExpiry"></el-table-column>
				<el-table-column label="证书状态" align="center" prop="enabled">
          <template #default="scope">
            <dict-tag :options="public_cert_status" :value="scope.row.enabled" />
          </template>
				</el-table-column>

				<el-table-column label="操作" width="250" align="center" class-name="small-padding fixed-width">
					<template #default="scope">
            <template v-if="scope.row.step == 1">
              <el-tooltip content="密钥参数配置" placement="top">
                <el-button
                  link
                  type="primary"
                  icon="Edit"
                  v-hasPermi="['bus:codeManagementConfig:edit']"
                  @click="handleUpdate(scope.row, 0)">
                </el-button>
              </el-tooltip>
            </template>
            
            <template v-if="scope.row.step == 2">
              <el-tooltip content="申请文件制作" placement="top">
                <el-button
                  link
                  type="primary"
                  icon="Edit"
                  v-hasPermi="['bus:codeManagementConfig:edit']"
                  @click="handleUpdate(scope.row, 1)">
                </el-button>
              </el-tooltip>
            </template>

            <template v-if="scope.row.step == 3">
              <el-tooltip content="下载申请文件" placement="top">
                <el-button
                  link
                  type="primary"
                  icon="Download"
                  v-hasPermi="['bus:codeManagementConfig:edit']"
                  @click="handleUpdate(scope.row, 2)">
                </el-button>
              </el-tooltip>
            </template>
            
						<template v-if="scope.row.step == 4">
              <el-tooltip content="公钥证书上传" placement="top">
                <el-button
                  link
                  type="primary"
                  icon="Upload"
                  v-hasPermi="['bus:codeManagementConfig:edit']"
                  @click="handleUpdate(scope.row, 3)">
                </el-button>
              </el-tooltip>
            </template>

            <template v-if="scope.row.step == 5">
              <el-tooltip content="查看详情" placement="top">
                <el-button
                  link
                  type="primary"
                  icon="View"
                  v-hasPermi="['bus:codeManagementConfig:query']"
                  @click="handleDetail(scope.row)">
                </el-button>
              </el-tooltip>
            </template>

            <template v-if="scope.row.enabled == 2 || scope.row.enabled == 4">
              <el-tooltip content="启用证书" placement="top">
                <el-button
                  link
                  type="primary"
                  icon="Unlock"
                  v-hasPermi="['bus:codeManagementConfig:edit']"
                  @click="handleStatusChange(scope.row)">
                </el-button>
              </el-tooltip>
            </template>

						<template v-if="scope.row.enabled == 3">
              <el-tooltip content="禁用证书" placement="top">
                <el-button
                  link
                  type="primary"
                  icon="Lock"
                  v-hasPermi="['bus:codeManagementConfig:edit']"
                  @click="handleStatusChange(scope.row)">
                </el-button>
            </el-tooltip>
            </template>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<pagination
			v-show="total > 0"
			:total="total"
			v-model:page="queryParams.pageNum"
			v-model:limit="queryParams.pageSize"
			@pagination="getList" />
	</div>
</template>

<script setup name="CertificateManagement">
import { listCertificateManagement } from '@/api/bus/certificateManagement'
import { updateCodeManagementConfig } from '@/api/bus/PlatCodeManagement'

const router = useRouter()
const { proxy } = getCurrentInstance()
const { public_cert_status } = proxy.useDict('public_cert_status')

const itemList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)

const data = reactive({
	queryParams: {
		pageNum: 1,
		pageSize: 10,
		requestNum: null,
		enabled: null
	}
})

const { queryParams } = toRefs(data)

/** 查询码方案列表 */
function getList() {
	loading.value = true
	listCertificateManagement(queryParams.value).then((response) => {
		itemList.value = response.rows
		total.value = response.total
		loading.value = false
	})
}

/** 搜索按钮操作 */
function handleQuery() {
	queryParams.value.pageNum = 1
	getList()
}

/** 重置按钮操作 */
function resetQuery() {
	proxy.resetForm('queryRef')
	handleQuery()
}

/** 状态修改 */
function handleStatusChange(row) {
	const flag = row.enabled == 2 || row.enabled == 4
	const text = flag ? '启用' : '禁用'
	proxy.$modal
		.confirm('确认要"' + text + '"吗?')
		.then(function () {
			const data = {
				enabled: flag ? 3 : 4,
				id: row.id
			}
			if (flag) {
				data.reqType = 'editCa' // 固定参数
			}
			return updateCodeManagementConfig(data)
		})
		.then(() => {
			proxy.$modal.msgSuccess('操作成功')
			getList()
		})
		.catch(function () {})
}

/** 新增按钮操作 */
function handleAdd() {
	router.push({
		name: 'CertificateManagementEdit'
	})
}

/** 修改按钮操作 */
function handleUpdate(row, active) {
	const num = row.requestNum || 0
	const date = row.caPubExpiry || 0
	router.push({
		name: 'CertificateManagementEdit',
		query: { id: row.id, active, back: 1, num, date }
	})
}

/** 详情按钮操作 */
function handleDetail(row) {
	router.push({
		name: 'CertificateManagementDetail',
		params: { id: row.id }
	})
}

getList()
</script>
